﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using System.Web.UI;
using System.Xml;
using Common.Utilities;
using Workflows.Components.Rules.Properties;

namespace Workflows.Components.Rules
{
    /// <summary>
    /// global variables
    /// </summary>
    [Serializable]
    public class RuleOutput:RuleNodeDataBase 
    {
        
        private string[] _OutputFieldNames;
        /// <summary></summary>
        public string[] OutputFieldNames
        {
            get
            {
                return this._OutputFieldNames;
            }
            set
            {
                this._OutputFieldNames = value;
            }
        }

       
        /// <summary>
        /// 
        /// </summary>
        public RuleOutput()
        {
            this.OutputFieldNames = null;
            this.Name = "Output";
            this.Description = string.Empty;
            this.Img = Resources.ApproveForm;
            this.RuleNodeType = Rules.RuleNodeType.Output;
        }

        public override System.Xml.XmlNode Serialize(ref System.Xml.XmlDocument xDoc, ref System.Xml.XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);
            if (this.OutputFieldNames != null && this.OutputFieldNames.Length > 0)
            {
                XmlNode fieldRootNode = XmlDataUtil.AddElement(ref xDoc, xNode, "Fields");
                foreach(string fieldName in this.OutputFieldNames)
                {
                    XmlNode fieldNode = XmlDataUtil.AddElement(ref xDoc, fieldRootNode, "Field");
                    XmlDataUtil.UpdateAttribute(ref xDoc, fieldNode, "FieldName",fieldName);
                }
            }
            return xNode;
        }

        public override RuleNodeDataBase Instantiate(XmlNode xNode)
        {
            RuleOutput ruleOutput=new RuleOutput();
            ruleOutput.RuleNodeID=new Guid(xNode.Attributes.GetNamedItem("RuleNodeID").Value);
            ruleOutput.Name = xNode.Attributes.GetNamedItem("Name").Value;
            ruleOutput.Description = xNode.Attributes.GetNamedItem("Description").Value;
            ruleOutput.RuleNodeType = (RuleNodeType) int.Parse(xNode.Attributes.GetNamedItem("RuleNodeType").Value);
            ruleOutput.OutputFieldNames = null;
            XmlNodeList fieldNodes = xNode.SelectNodes("Fields/Field");
            if(fieldNodes !=null && fieldNodes.Count>0)
            {
                List<string> fieldNames=new List<string>();
                foreach(XmlNode fieldNode in fieldNodes)
                {
                    fieldNames.Add(fieldNode.Attributes.GetNamedItem("FieldName").Value);
                }
                ruleOutput.OutputFieldNames = fieldNames.ToArray();
            }
            return ruleOutput;
        }

        public override string DescribeInHtml()
        {
            StringBuilder buffer = new StringBuilder();
            HtmlTextWriter htmlWriter = new HtmlTextWriter(new StringWriter(buffer));
            htmlWriter.RenderBeginTag(HtmlTextWriterTag.Div);
            htmlWriter.RenderBeginTag(HtmlTextWriterTag.B);
            htmlWriter.RenderBeginTag(HtmlTextWriterTag.U);
            htmlWriter.Write(string.Format("Output {0}:", this.RuleNodeID.ToString()));
            htmlWriter.RenderEndTag();
            htmlWriter.RenderEndTag();
            htmlWriter.RenderBeginTag(HtmlTextWriterTag.Br);
            htmlWriter.RenderEndTag();
            htmlWriter.RenderBeginTag(HtmlTextWriterTag.Ul);
            if (this.OutputFieldNames != null)
            {
                foreach (string name in this.OutputFieldNames)
                {
                    htmlWriter.RenderBeginTag(HtmlTextWriterTag.Li);
                    htmlWriter.Write(string.Format("field: {0}", name));
                    htmlWriter.RenderEndTag();
                }
            }
            else
            {
                htmlWriter.RenderBeginTag(HtmlTextWriterTag.Li);
                htmlWriter.Write("Output is not initialized");
                htmlWriter.RenderEndTag();
            }
            htmlWriter.RenderEndTag();
            htmlWriter.RenderEndTag();
            return buffer.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string Describe()
        {
            StringBuilder buffer=new StringBuilder();
            buffer.Append("Output " + this.RuleNodeID.ToString());
            buffer.Append(Environment.NewLine);
            if(this.OutputFieldNames !=null && this.OutputFieldNames.Length>0)
            {
                foreach(string fieldName in this.OutputFieldNames)
                {
                    buffer.Append(fieldName + Environment.NewLine);
                }
            }
            else
            {
                buffer.Append("Output field is not initialized.");
            }
            return buffer.ToString();
        }
    }
}
